package com.friday.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.dao.AgentDao;
import com.friday.util.UploadImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class AgentController {

    @Autowired
    private AgentDao agentDao;

    public JSONObject getAgentById(int id){
        return agentDao.getAgentById(id);
    }

    @RequestMapping("/agentList")
    public JSONArray getAllAgent(){
        return agentDao.getAllAgent();
    }

    @RequestMapping("/agentEdit")
    public String editAgent(@RequestBody JSONObject message){
        int tag = agentDao.editAgent(message);
        if(tag>0){
            return "success";
        }else{
            return "fail";
        }
    }

    @RequestMapping("/agentAvatarEdit")
    public JSONObject editAvatar(int id, MultipartFile file){
        //删除原来的图片
        JSONObject agent=agentDao.getAgentById(id);
        UploadImg.delete(agent.getString("avatar"));

        //放入新的照片
        String url=UploadImg.upload(file);
        JSONObject avatar=new JSONObject();
        avatar.put("id",id);
        avatar.put("avatar",url);
        JSONObject result=new JSONObject();
        int tag=agentDao.editAgent(avatar);
        if(tag>0)
        {
            agent=agentDao.getAgentById(id);
            result.put("result","success");
            result.put("user",agent);
        }else
        {
            result.put("result","fail");
            result.put("user",null);
        }
        return result;
    }



    @RequestMapping("/agentLogin")
    public JSONObject checkLogin(@RequestParam("id") int id,@RequestParam("password") String password){
        JSONObject result=new JSONObject();
        //返回是否查到
        int ifExist=agentDao.checkExist(id,password);
        if(ifExist>0)
        {
            //若查到，则返回成功信息
            JSONObject agent=agentDao.getAgentById(id);
            result.put("result","success");
            result.put("user",agent);
        }else{
            //若未查到，返回失败信息
            result.put("result","fail");
            result.put("user",null);
        }
        return result;
    }




    @RequestMapping("/agentAdd")
    public JSONObject addAgent(@RequestBody JSONObject agent){
        JSONObject result = new JSONObject();
        int tag = agentDao.addAgent(agent);
        if( tag>0 ){
            result.put("result","success");
            result.put("agent_id",agent.getInteger("id"));
        }else {
            result.put("result","fail");
            result.put("agent_id",null);
        }
        return result;
    }

    @RequestMapping("/agentDelete")
    public String delAgentById(@RequestParam("id") int id){
        int tag= agentDao.delAgentById(id);
        if(tag>0){
            return "success";
        }else{
            return "fail";
        }
    }


    public String changePassword(JSONObject message){
        int tag = agentDao.changePassword(message);
        if(tag>0){
            return "success";
        }else{
            return "fail";
        }
    }








}
